package com.sinog.basic.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.basic.mapper.DepartmentMapper;
import com.sinog.basic.mapper.PositionMapper;
import com.sinog.basic.service.PositionService;
import com.sinog.core.domain.Department;
import com.sinog.core.domain.Position;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.util.StrUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @desc 系统管理 - 岗位管理 - 岗位管理 - PositionServiceImpl
 * @author hak
 * @date -
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper,Position> implements PositionService {

    @Resource
    private PositionMapper positionMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 保存岗位信息
     * @param data data
     * @return Result
     */
    @Override
    public Result save(String data) {
        Position bean = JsonUtil.toBean(data,Position.class);
        boolean tag = bean.insertOrUpdate();
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 获取全部岗位
     * @param reskey reskey
     * @param loginUserLevel loginUserLevel
     * @return Result
     */
    @Override
    public Result selectAllPosition(String reskey,String loginUserLevel) {
        //根据机构ID（reskey）查询级别
        Department department = departmentMapper.selectByDeptId(reskey);
        List<Map<String,String>> allPositionList = positionMapper.selectListPosition(department.getLevel());
        if(allPositionList.isEmpty()) {
            Result.failure(ReturnCode.SELECT_FAILED);
        }
        return Result.success(allPositionList);
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 删除资源
     * @param id id
     * @return Result
     */
    @Override
    public Result deleteOne(String id) {
        Position position = new Position();
        position.setId(id);
        boolean result = position.deleteById(id);
        if(result) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 根据ID查询岗位详情
     * @param id id
     * @return Result
     */
    @Override
    public Result selectOne(String id) {
        Position position = new Position();
        position.setId(id);
        Position res = position.selectById();
        if(null != res) {
            return Result.success(res);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 条件获取岗位列表
     * @param positionkey positionkey
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param loginUserLevel loginUserLevel
     * @return String
     */
    @Override
    public String selectPositionList(String positionkey,Integer pageIndex,Integer pageSize,String loginUserLevel) {
        positionkey = StrUtil.converseToNull(positionkey);
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        page = page.setRecords(positionMapper.selectPositionList(page,positionkey,loginUserLevel));
        return PageReturnUtil.pageReturn(page);
    }
}